<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>For循环优化</title>
</head>
<body>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>
    <p class="btn">add</p>


    <script>
        const btns = document.getElementsByClassName('btn')

        for(var i = 0; i < btns.length; i++){
            console.log(i)
        }

        // 在每次执行循环的时候， 将btns.length保存给变量len
        // 从而提高循环效率
        for(var j = 0, len=btns.length;  j < len; j++){
            console.log(j)
        }

    </script>
</body>
</html>